package com.chain.qs.modules.biz.web;

import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.modules.biz.entity.*;
import com.chain.qs.modules.biz.service.*;
import com.chain.qs.modules.sys.entity.Office;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "${adminPath}/biz/eiwRule")
public class EquipmentItemWarningRuleController extends SeniorController {

    @Resource
    private StationService stationService;
    @Resource
    private EquipmentGroupItemService equipmentGroupItemService;
    @Resource
    private EquipmentItemWarningRuleService equipmentItemWarningRuleService;
    @Resource
    private EventTemplateService eventTemplateService;
    @Resource
    private EquipmentParameterService equipmentParameterService;

    @ModelAttribute
    public EquipmentItemWarningRule get(@RequestParam(required = false) String id, Model model, String sgroupId, String itemId) {
        EquipmentItemWarningRule equipmentItemWarningRule;
        if (StringUtils.isNotBlank(id)) {
            equipmentItemWarningRule = equipmentItemWarningRuleService.get(id);
            model.addAttribute("sgroup", sgroupService.get(equipmentItemWarningRule.getSgroupId()));
            model.addAttribute("eitem", equipmentGroupItemService.get(equipmentItemWarningRule.getItemId()));
        } else {
            equipmentItemWarningRule = new EquipmentItemWarningRule();
            if (sgroupId != null && !sgroupId.equals("")) {
                equipmentItemWarningRule.setSgroupId(sgroupId);
                model.addAttribute("sgroup", sgroupService.get(sgroupId));
            }
            if (itemId != null && !itemId.equals("")) {
                equipmentItemWarningRule.setItemId(itemId);
                model.addAttribute("eitem", equipmentGroupItemService.get(itemId));
            }
        }
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return equipmentItemWarningRule;
    }

    @ModelAttribute("condition")
    public Condition getCondition(String spare1,String spare2) {
        Condition condition = new Condition();
        if (spare1 != null && !spare1.equals("")) {
            condition.setSpare1(spare1);
        }
        if (spare2 != null && !spare2.equals("")) {
            condition.setSpare2(spare2);
        }
        return condition;
    }

    @RequestMapping(value = "")
    public String firstPage() {
        return "modules/biz/equItemWarningRulePre";
    }

    @RequestMapping(value = "list")
    public String firstPage(Model model, EquipmentItemWarningRule equipmentItemWarningRule) {
        model.addAttribute("eiwRuleList",equipmentItemWarningRuleService.findList(equipmentItemWarningRule));
        return "modules/biz/equItemWarningRuleList";
    }


    @RequestMapping(value = "form")
    public String form(Model model, String itemId) {
        if (itemId != null && !itemId.equals("")) {
            model.addAttribute("paramList", equipmentParameterService.paramList(equipmentGroupItemService.get(itemId).getEquipment().getId()));
        }
        return "modules/biz/equItemWarningRuleForm";
    }

    @RequestMapping(value = "addThroughTemplateButton")
    public String addThroughTemplateButton(Model model,String dataLength, String itemId){
        if (itemId != null && !itemId.equals("")) {
            String equipmentId = equipmentGroupItemService.get(itemId).getEquipment().getId();
            model.addAttribute("paramList", equipmentParameterService.paramList(equipmentId));
            model.addAttribute("templateList",eventTemplateService.findListByEquipId(equipmentId));
        }
        model.addAttribute("dataLength",dataLength);
        return "modules/biz/equItemWarningRuleFormTemplate";
    }

    @ResponseBody
    @RequestMapping(value = "addThroughTemplate")
    public String addThroughTemplate(String spare2, String sgroupId, String itemId){//spare2：模板ID
        equipmentItemWarningRuleService.saveEquipmentItemWarningRuleList(spare2,itemId,sgroupId);
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public String save(EquipmentItemWarningRule equipmentItemWarningRule){
        equipmentItemWarningRuleService.save(equipmentItemWarningRule);
        return "success";
    }

    @RequestMapping(value="delete")
    public String delete(EquipmentItemWarningRule equipmentItemWarningRule,String officeId, String sgroupId, String itemId, RedirectAttributes redirectAttributes){
        equipmentItemWarningRuleService.delete(equipmentItemWarningRule);
        addMessage(redirectAttributes,"删除告警规则成功");
        return "redirect:"+adminPath+"/biz/eiwRule/list?officeId="+officeId+"&sgroupId="+sgroupId+"&itemId=" +itemId;
    }

}
